/**
 * Display lab queue with patientType filter.
 * @param patientType
 * @return
 */
function viewLabQueue(patientType) {
	$.ajax({
		type:		"GET",
		url:		"../LabQueue/RenderQueueAjax?set_ajax_view",
		data:		"patientType=" + patientType,		
		dataType:   "json",
		beforeSend: function() {
			showAjaxLoader('reload', 'innerLoading', false);
		},
		success: 	function(data) {
			if (data.error.code == 'OK') {	
				$("table#tableWaitingQueue tbody").html(data.result.waiting.body);
				$("table#tableWaitingQueue tr:last-child").addClass('last');
				$("table#tableInProcessQueue tbody").html(data.result.inprocess.body);
				$("table#tableInProcessQueue tr:last-child").addClass('last');
				$("table#tableWaitingQueue").trigger('update');	
				$("table#tableInProcessQueue").trigger('update');	
				fillTotalTableData($('#waitingQueue'),data.result.waiting.total_table);
				fillTotalTableData($('#inProcessQueue'),data.result.inprocess.total_table);
			} else {
				displayError(data.error.mess);				
			}
			showAjaxLoader('reload', 'innerLoading', true);
		}
	});
}

function fillTotalTableData(parentDiv, data) {
	$('table.total_table', parentDiv).remove();
	parentDiv.append(data);
	parentDiv.children('table:last-child').addClass('total_table');
}

/**
 * move a patient from waiting queue to processing queue  
 * @param labAssignmentTicketId
 * @return
 */
function startProcessingQueueItem(labAssignmentTicketId) {
	$.ajax ({
		type	: "GET",
		url		: "../LabQueue/StartProcessQueueItemAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId,
		dataType: 'json',
		success : function(data) {
			if (data.error.code == 'OK') {	
				//refresh queue
				$('#reloadLabQueue').trigger('click');	
			} else {
				displayError(data.error.mess);				
			}
		}
	});
}

function cancelProcessingQueueItem(patientName, labAssignmentTicketId){
	promptConfirm(_("Move back waiting queue"),
			_('Are you sure moving the assignment of patient[{0}] back to waiting queue?', patientName), function() {
		submitCancelProcessingQueueItem(labAssignmentTicketId); 
		return true;
	},'YesNo','warn',_('No'));
}

function submitCancelProcessingQueueItem(labAssignmentTicketId) {

	$.ajax ({
		type	: "GET",
		url		: "../LabQueue/CancelProcessingQueueItemAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId,
		dataType: 'json',
		success : function(data) {
			if (data.error.code == 'OK') {	
				//refresh queue
				$('#reloadLabQueue').trigger('click');	
			} else {
				displayError(data.error.mess);				
			}
		}
	});	
}

function finishProcessingQueueItem(patientName, labAssignmentTicketId){
	$.ajax ({
		type	: "GET",
		url		: "../LabQueue/PreFinishProcessingQueueItemAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId,
		dataType: 'json',
		success : function(data) {
			if (data.error.code == 'OK') {	
				//refresh queue				
				if ($.trim(data.result) == '') {
					promptConfirm(_("Finish"),
							_('Are you sure the assignment of patient[{0}] finished?', patientName), function() {
						submitFinishProcessingQueueItem(labAssignmentTicketId); 
						return true;
					},'YesNo','warn',_('No'));	
				} else {
					promptConfirm(_("Finish"),
							_('There are some ticket[{0}] not have result yet. Are you sure the assignment of patient[{1}] finished?', data.result, patientName), function() {
						submitFinishProcessingQueueItem(labAssignmentTicketId); 
						return true;
					},'YesNo','warn',_('No'));
					
				}
			} else {
				displayError(data.error.mess);				
			}
		}
	});
}

function submitFinishProcessingQueueItem(labAssignmentTicketId) {
	$.ajax ({
		type	: "GET",
		url		: "../LabQueue/FinishProcessingQueueItemAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId,
		dataType: 'json',
		success : function(data) {
			if (data.error.code == 'OK') {	
				//refresh queue
				$('#reloadLabQueue').trigger('click');	
			} else {
				displayError(data.error.mess);				
			}
		}
	});	
}

function viewInputLabResultPage(labAssignmentTicketId, selectedLabTicketId) {
	window.location = "../LabResult/ViewInputLabResultPage?labAssignmentTicketId=" + labAssignmentTicketId
				+ "&selectedLabTicketId=" + selectedLabTicketId;
}